$(function() {
    //   随机验证码
    $(".middle_gray").on("click", function() {
        var winChild = window.open("./child.html", "_blank",
            "width=567,height=500,left=520px,top=120");

    })

    $.idcode.setCode()

    //   随机验证码结束

    // 登录注册正则验证
    $("#uname").on("focus", function() {
        // console.log($("input[name='uname']"));
        $("#phoneform").css("display", "block")


    })


    $("#uname").on("blur", function() {
        $("#phoneform").css("display", "none")
        var phNumber = $(this).val()
        var re = /1(31|32|34|35|36|37|38|39|50|57|58|86|87|88)[0-9]{8}/g;
        var wrResult = re.test(phNumber)
        if (wrResult == true && phNumber.length == 11) {
            $("#phoneform").css("display", "block")
            $("#phoneform").html("格式正确")
            $("#phoneform").css("color", "green")
        } else if (phNumber == '') {
            $("#phoneform").css("display", "block")
        } else {
            $("#phoneform").css("display", "block")
            $("#phoneform").html('请输入正确的手机号')
            $("#phoneform").css("color", "red")
            $("#uname").val("")
        }
    })
    $("#uupwd").on("focus", function() {

        $("#upwdform").css("display", "block")
    })
    $("#uupwd").on("blur", function() {
        $("#upwdform").css("display", "none")
        var pw1Num = $(this).val()
        var re = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/g;
        var rez = re.test(pw1Num)
        if (rez == true) {
            $("#upwdform").css("display", "block")
            $("#upwdform").html("格式正确")
        }
        if ($(this).val() != "") {
            $("#upwdform").css("display", "block")
            $("#upwdform").html("格式正确")
            $("#upwdform").css("color", "green")
        } else {
            $("#upwdform").css("display", "block")
            $("#upwdform").html("密码不能为空")
            $("#upwdform").css("color", "red")
        }
        // else {
        //     $("#upwdform").css("display", "block")
        // }
    })

    $("#upwd1").on("focus", function() {

        $("#upwdform1").css("display", "block")
    })
    $("#upwd1").on("blur", function() {
        if ($(this).val() == $("#uupwd").val() && $(this).val() != null) {

            $("#upwdform1").css("display", "block")
            $("#upwdform1").html("两次输入一致")
        } else {
            $("#upwdform1").css("display", "block")
            $("#upwdform1").html("两次输入不一致")
            $(this).val('')
        }
        if ($(this).val() = null) {
            $("#upwdform1").css("display", "block")
            $("#upwdform1").html("不能为空")
        }

    })






    // 登录注册正则验证结束

    // console.log($("form"));
    // 注册
    $("form").on("submit", function() {
        var formdata = {
            uname: $('[name="uname"]').val(),
            upwd: $('[name="upwd"]').val(),
            uyz: $('[name="utext"]').val()
        }
        console.log(formdata);

        $.ajax({
            url: "/api/reg",
            data: formdata,
            type: "post",
            // processData: false,
            // contentType: false
        }).then(function(res) {
            if (res.status == 1) {
                layer.msg("注册成功", {
                    icon: 6
                })
                if (layer.msg) {
                    setTimeout(() => {
                        window.location.href = "DL.html";
                    }, 2000);
                }
            } else {
                layer.msg("注册失败", {
                    icon: 5

                })

            }
        })



        return false


    })

})